/**  
 * 文 件 名: UserMgrAction.java  
 * 描    述：   
 * 版    权：Copyright (c)20012-2012 tongzhou All Rights Reserved.
 * 公    司: 同舟 
 * 作    者：彭定雄                     
 * 版    本: 1.0  
 * 创建时间: 2012-10-27
 *  
 * 修改历史：  
 * 时间                             作者                       版本                        描述  
 * ------------------------------------------------------------------  
 * 2012-10-27        彭定雄                1.0          1.0 Version  
 */         

/**
 * 
 */
package com.tongzhou.system.userMgr.action;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.system.rightsMgmt.dao.IRoleDAO;
import com.tongzhou.system.rightsMgmt.model.Role;
import com.tongzhou.system.userMgr.dao.IUserMgrDAO;
import com.tongzhou.system.userMgr.model.Department;
import com.tongzhou.system.userMgr.model.SearchUserCondition;
import com.tongzhou.system.userMgr.model.User;
import com.tongzhou.util.DES;
import com.tongzhou.util.Util;

public class UserMgrAction extends ActionSupport{
	private String id;
	private String username;
	private String password;
	private String name;
	private String sex;
	private String email;
	private String qq;
	private String mobile;
	private String telephone;
	private String dept_id;
	private String address;
	private String active;
	private String create_time;
	private String create_by;
	private String update_time;
	private String update_by;
	private String dept_name;
	private HttpServletResponse response;
	private Map<String, Object> session;
	private User user;
	private List<User> result=null;
	private SearchUserCondition condition=null;
	private List<Department> deptList;
	private List<Role> roleList;
	private List<Role> userRoleList;
	
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private String judgment;
	private String itemsVal;
	
	private int existUser;
	
	private IUserMgrDAO dao=null;
	private IRoleDAO roleDAO;
	/**
	 * 用户列表界面
	 *@author: 彭定雄
	 *@return 
	 *@throws DBException
	 */
	public String listUser() throws DBException{
		checkSearchCondition();
		condition = new SearchUserCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setCurrentPage(currentPage) ;
		condition.setUsername(username);
		condition.setDept_name(dept_name);
		condition.setName(name);
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType) ;
		condition.setPageSize(pageSize) ;
	    result = dao.listUser(condition);
		return "listUser";
	} 
	
	public String listUserRole() throws DBException{
		checkSearchCondition();
		condition = new SearchUserCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setCurrentPage(currentPage) ;
		condition.setUsername(username);
		condition.setDept_name(dept_name);
		condition.setName(name);
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType) ;
		condition.setPageSize(pageSize) ;
	    result = dao.listUser(condition);
		return "listUserRole";
	}
	
	public String toEditUserRole() throws DBException{
		user=new User();
		user.setId(id);
		userRoleList=dao.getUserRoleByUserId(user);
		roleList=roleDAO.getAllRole();
		return "toEditUserRole";
	}
	
	public String editUserRole() throws DBException{
		user=new User();
		user.setId(id);
		user.setItemsVal(itemsVal);
		dao.updateUserRole(user);
		listUser();
		return "listUserRole";
	}
	
	/**
	 * 用户修改秘密
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toChangePwdByUser() throws DBException{
		return "changePwdByUser";
	}
	
	public String changePwdByUser() throws DBException{
		user=new User();
		user.setId(id);
		user.setPassword(DES.encrypt(password));
		user.setUpdate_by(id);
		dao.changePwdByUser(user);
		return SUCCESS;
	}
	
	/**
	 * 管理员修改秘密
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toChangePwdByMgmt() throws DBException{
		return "changePwdByMgmt";
	}
	
	public String changePwdByMgmt() throws DBException{
		user=new User();
		try {
			user.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		user.setPassword(DES.encrypt(password));
		user.setUpdate_by(id);
		user.setActive(active);
		dao.changePwdByMgmt(user);
		return SUCCESS;
	}
	
	public String checkUserByName() throws DBException{
		user=new User();
		try {
			user.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result=dao.checkUserByName(user);
		if(result.size()>0){
			judgment="0";
		}else{
			judgment="1";
		}
		return SUCCESS;
	}
	
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "desc" ;
		}
	}
	/**
	 * 新增用户
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toAddUser() throws DBException{
		return "toAddUser";
	}
	
	public String addUser() throws DBException{
		session=ActionContext.getContext().getSession();
		response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		user=new User();
		try {
			user.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		user.setPassword(DES.encrypt(password));
		user.setCreate_by(String.valueOf(Util.getAdminUserId()));
		user.setActive(active);
		dao.addUser(user);
		username="";
		listUser();
		return "listUser";
	}
	
	public String deleteUser() throws DBException{
		user=new User();
		user.setId(id);
		
//		dao=new UserMgrDAO();
		dao.deleteUser(user);
		listUser();
		return "listUser";
	}
	
	public String toEditUser() throws DBException{
		user=new User();
		user.setId(id);
		result=dao.getUserById(user);
		if(result.size()>0){
			user=result.get(0);
		}
		deptList=dao.getAllDepartment();
		return "toEditUser";
	}
	
	public String editUser() throws DBException{
		user=new User();
		user.setId(id);
		user.setUsername(username);
		user.setName(name);
		user.setSex(sex);
		user.setEmail(email);
		user.setQq(qq);
		user.setMobile(mobile);
		user.setTelephone(telephone);
		user.setDept_id(dept_id);
		user.setAddress(address);
		
		dao.updateUser(user);
		username="";
		this.addActionMessage("修改成功！");
		return toEditUser();
	}
	
	public int existUser(User user) throws DBException{
		existUser=0;
//		dao=new UserMgrDAO();
		result=dao.getUserByName(user);
		if(result.size()>0){
			existUser=1;
		}
		return existUser;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getDept_id() {
		return dept_id;
	}

	public void setDept_id(String deptId) {
		dept_id = deptId;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getActive() {
		return active;
	}

	public void setActive(String active) {
		this.active = active;
	}

	public String getCreate_time() {
		return create_time;
	}

	public void setCreate_time(String createTime) {
		create_time = createTime;
	}

	public String getCreate_by() {
		return create_by;
	}

	public void setCreate_by(String createBy) {
		create_by = createBy;
	}

	public String getUpdate_time() {
		return update_time;
	}

	public void setUpdate_time(String updateTime) {
		update_time = updateTime;
	}

	public String getUpdate_by() {
		return update_by;
	}

	public void setUpdate_by(String updateBy) {
		update_by = updateBy;
	}
	public List<User> getResult() {
		return result;
	}
	public void setResult(List<User> result) {
		this.result = result;
	}
	public SearchUserCondition getCondition() {
		return condition;
	}
	public void setCondition(SearchUserCondition condition) {
		this.condition = condition;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getOrderByType() {
		return orderByType;
	}

	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}

	public IUserMgrDAO getDao() {
		return dao;
	}

	public void setDao(IUserMgrDAO dao) {
		this.dao = dao;
	}

	public String getDept_name() {
		return dept_name;
	}

	public void setDept_name(String deptName) {
		dept_name = deptName;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public int getExistUser() {
		return existUser;
	}

	public void setExistUser(int existUser) {
		this.existUser = existUser;
	}
	public String getJudgment() {
		return judgment;
	}
	public void setJudgment(String judgment) {
		this.judgment = judgment;
	}
	public List<Department> getDeptList() {
		return deptList;
	}
	public void setDeptList(List<Department> deptList) {
		this.deptList = deptList;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public List<Role> getUserRoleList() {
		return userRoleList;
	}

	public void setUserRoleList(List<Role> userRoleList) {
		this.userRoleList = userRoleList;
	}

	public String getItemsVal() {
		return itemsVal;
	}

	public void setItemsVal(String itemsVal) {
		this.itemsVal = itemsVal;
	}

	public IRoleDAO getRoleDAO() {
		return roleDAO;
	}

	public void setRoleDAO(IRoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}
}
